
uniform sampler2D combine;
uniform sampler2D texture0;
uniform sampler2D texture1;
uniform sampler2D texture2;

uniform sampler2D map_shadow0;


varying vec2 tex_coord;
varying vec4 color;

varying vec4 screen_coord;

float get_shadow_value ()
{
    vec2 sc = (screen_coord / screen_coord.w).xy * 0.5 + 0.5;
    vec2 sd = vec2 (2.0 / 1024.0, 2.0 / 768.0);

    return  (texture2D (map_shadow0, sc + vec2(-sd.x, -sd.y)).x +
             texture2D (map_shadow0, sc + vec2( sd.x, -sd.y)).x +
             texture2D (map_shadow0, sc + vec2( sd.x,  sd.y)).x +
             texture2D (map_shadow0, sc + vec2(-sd.x,  sd.y)).x) / 4.0;
}

void main()
{
    vec4 cmb = texture2D(combine, tex_coord);
    gl_FragColor = color * (
        texture2D(texture0, tex_coord * vec2(32.0, 32.0)) * cmb.r +
        texture2D(texture1, tex_coord * vec2(32.0, 32.0)) * cmb.g +
        texture2D(texture2, tex_coord * vec2(32.0, 32.0)) * cmb.b
    );

    vec2 sc = (screen_coord / screen_coord.w).xy * 0.5 + 0.5;
    gl_FragColor.xyz *= get_shadow_value (); //texture2D(map_shadow0, sc);

    float d = (gl_FragColor.r + gl_FragColor.g + gl_FragColor.b) / 3.0;
    gl_FragColor.rgb = gl_FragColor.rgb + 0.5 * (vec3(d,d,d) - gl_FragColor.rgb);

}
